package cn.xfdzx.android.apps.shop.fragment.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.adapter.HomeNearShopAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.NearShopBeen;
import cn.xfdzx.android.apps.shop.contract.HomeNearShopContract;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.evenbus.FoundGoodSelectIdMessage;
import cn.xfdzx.android.apps.shop.evenbus.HomeAdressMessage;
import cn.xfdzx.android.apps.shop.evenbus.NearShopSelectMessage;
import cn.xfdzx.android.apps.shop.presenter.HomeNearShopPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

public class RetailNearShopFragment extends BaseMvpFragment<HomeNearShopPresenter> implements HomeNearShopContract.View {


    private RecyclerView rvShops;
    private Context mContext;
    public HomeNearShopAdapter homeNearShopAdapter;
    private String lng = "";
    private String lat = "";
    private String distance = "20";
    private String sort = "pinkage";
    private int page = 1;
    private int limit = Constants.LIMIT;
    private List<NearShopBeen.DataBean.DataListBean> mList = new ArrayList<>();
    SmartRefreshLayout mRefreshLayout;
    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);
        mContext=getActivity();
        findViewInit(view);

        mPresenter = new HomeNearShopPresenter();
        mPresenter.attachView(this);
    }

    private void findViewInit(View view) {
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        rvShops=view.findViewById(R.id.rv_shops);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvShops.setLayoutManager(linearLayoutManager);
        rvShops.setNestedScrollingEnabled(false);
        rvShops.setItemAnimator(new DefaultItemAnimator());
        homeNearShopAdapter = new HomeNearShopAdapter(mContext);

        rvShops.setAdapter(homeNearShopAdapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_retail_near_shop;
    }

    @Override
    public void showLoading() {
        if (TYPE_IS_RETAIL == 0) {
            if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
                ProgressDialog.getInstance().show(getActivity());
            }
        }
    }

    @Override
    public void hideLoading() {
        if (TYPE_IS_RETAIL == 0) {
            ProgressDialog.getInstance().dismiss();
        }
    }
    //更新地址
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(HomeAdressMessage messageEvent) {
        Log.e("messageEvent",messageEvent.getLng()+"--经纬度--"+messageEvent.getLat());
        lng=  messageEvent.getLng();
        lat=  messageEvent.getLat();
        UtilsData.getPreference(getActivity(), "coordinates").put("coordinates_lng",lng);
        UtilsData.getPreference(getActivity(), "coordinates").put("coordinates_lat",lat);
        page = 1;
        mPresenter.initNearData(true, true, lng, lat, distance, sort, page + "", limit + "");
    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(NearShopSelectMessage messageEvent) {
        lng=  UtilsData.getPreference(getActivity(), "coordinates").getString("coordinates_lng");
        lat= UtilsData.getPreference(getActivity(), "coordinates").getString("coordinates_lat");
        if (messageEvent.getMessage() == 1) {
            page = 1;
            sort = "pinkage";
            mPresenter.initNearData(true, true, lng, lat, distance, sort, page + "", limit + "");
        } else if (messageEvent.getMessage() == 2) {
            page = 1;
            sort = "monthly_sales";
            mPresenter.initNearData(true, true, lng, lat, distance, sort, page + "", limit + "");
        } else if (messageEvent.getMessage() == 3) {
            page = 1;
            sort = "distance";
            mPresenter.initNearData(true, true, lng, lat, distance, sort, page + "", limit + "");
        }
    }
    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitNearDataSuccess(NearShopBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (EmptyUtil.isNotEmpty(bean.getData())){
                if (page == 1) {
                    mList.clear();
                    homeNearShopAdapter.setmList(mList);
                    homeNearShopAdapter.setOnItemClickListener(new ItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            Intent intent = new Intent(getActivity(), ShopActivity.class);
                            intent.putExtra("shop_id", mList.get(position).getId() + "");
                            startActivity(intent);
                        }
                    });
                    mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                        @Override
                        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                            mPresenter.initNearData(false, false, lng, lat, distance, sort, page + "", limit + "");

                        }
                    });
                }

                if (EmptyUtil.isNotEmpty(bean.getData().getDataList())){
                    if (bean.getData().getDataList().size() == 0) {
                        mRefreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        page++;
                    }
                    mList.addAll(bean.getData().getDataList());
                }

                homeNearShopAdapter.notifyDataSetChanged();
            }else {
                mList.clear();
                homeNearShopAdapter.setmList(mList);
                homeNearShopAdapter.notifyDataSetChanged();
            }

        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    public static RetailNearShopFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        RetailNearShopFragment fragment = new RetailNearShopFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }
    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }
}
